pacman::p_load("car","data.table","fastDummies")
#additional packages and functions
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}


#Open data -- following GESIS instructions --------

#run this code to select variables
#selection<-c("z000001a", "z000011a", "cdze001a", "cdze002a", "cdze003a", "cdze004a", "cdze005a", "cdze006a", "cdze007a", "cdze008a", "cdze009a", "cdze010a", "edze001a", "edze002a", "edze003a", "edze004a", "edze005a", "edze006a", "edze007a", "edze008a", "edze009a", "edze010a", "gdze001a", "gdze002a", "gdze003a", "gdze004a", "gdze005a", "gdze006a", "gdze007a", "gdze008a", "gdze009a", "gdze010a", "cbzc061a", "ebzc061a", "gbzc064a", "bcaj063a", "efaj098a", "a11d082b", "d11d082a", "a11d056b", "d11d056b", "a11d054a", "d11d054a", "a11d097c", "d11d097b", "bfzh089c")

#run this code to subset data
#gesis<-datc[selection]

#save(gesis, "../GESIS/GESIS_selection.RData")

#load data
load("Data/GESIS_selection.RData")

#Openness 2015------
gesis$open15_1 <-6-car::recode(gesis$cdze005a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$open15_2 <-car::recode(gesis$cdze010a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
open <-data.frame(gesis$open15_1, gesis$open15_2)
gesis$open15_latent<-rowMeans(open, na.rm=T)

#Conscientiosness 2015------
gesis$con15_1 <-6-car::recode(gesis$cdze003a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$con15_2 <-car::recode(gesis$cdze008a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
con <-data.frame(gesis$con15_1, gesis$con15_2)
gesis$con15_latent<-rowMeans(con, na.rm=T)

#Extraversion 2015-----------
gesis$ext15_1 <-6-car::recode(gesis$cdze001a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$ext15_2 <-car::recode(gesis$cdze006a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
ext <-data.frame(gesis$ext15_1, gesis$ext15_2)
gesis$ext15_latent<-rowMeans(ext, na.rm=T)


#Agreeableness 2015-----------
gesis$agre15_1 <-car::recode(gesis$cdze002a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$agre15_2 <-6-car::recode(gesis$cdze007a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
agre <-data.frame(gesis$agre15_1, gesis$agre15_2)
gesis$agre15_latent<-rowMeans(agre, na.rm=T)

#Neuroticism 2015-----------
gesis$neu15_1 <-6-car::recode(gesis$cdze004a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$neu15_2 <-car::recode(gesis$cdze009a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
neu <-data.frame(gesis$neu15_1, gesis$neu15_2)
gesis$neu15_latent<-rowMeans(neu, na.rm=T)

#Openness 2017------
gesis$open17_1 <-6-car::recode(gesis$edze005a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$open17_2 <-car::recode(gesis$edze010a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
open <-data.frame(gesis$open17_1, gesis$open17_2)
gesis$open17_latent<-rowMeans(open, na.rm=T)

#Conscientiousness 2017----------------
gesis$con17_1 <-6-car::recode(gesis$edze003a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$con17_2 <-car::recode(gesis$edze008a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
con <-data.frame(gesis$con17_1, gesis$con17_2)
gesis$con17_latent<-rowMeans(con, na.rm=T)

#Extraversion 2017-----------
gesis$ext17_1 <-6-car::recode(gesis$edze001a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$ext17_2 <-car::recode(gesis$edze006a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
ext <-data.frame(gesis$ext17_1, gesis$ext17_2)
gesis$ext17_latent<-rowMeans(ext, na.rm=T)

#Agreeableness 2015-----------
gesis$agre17_1 <-car::recode(gesis$edze002a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$agre17_2 <-6-car::recode(gesis$edze007a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
agre <-data.frame(gesis$agre17_1, gesis$agre17_2)
gesis$agre17_latent<-rowMeans(agre, na.rm=T)

#Neuroticism 2017-----------
gesis$neu17_1 <-6-car::recode(gesis$edze004a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$neu17_2 <-car::recode(gesis$edze009a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
neu <-data.frame(gesis$neu17_1, gesis$neu17_2)
gesis$neu17_latent<-rowMeans(neu, na.rm=T)

#Openness 2019------
gesis$open19_1 <-6-car::recode(gesis$gdze005a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$open19_2 <-car::recode(gesis$gdze010a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
open <-data.frame(gesis$open19_1, gesis$open19_2)
gesis$open19_latent<-rowMeans(open, na.rm=T)

#Conscientiousness 2019----------------
gesis$con19_1 <-6-car::recode(gesis$gdze003a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$con19_2 <-car::recode(gesis$gdze008a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
con <-data.frame(gesis$con19_1, gesis$con19_2)
gesis$con19_latent<-rowMeans(con, na.rm=T)

#Extraversion 2019-----------
gesis$ext19_1 <-6-car::recode(gesis$gdze001a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$ext19_2 <-car::recode(gesis$gdze006a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
ext <-data.frame(gesis$ext19_1, gesis$ext19_2)
gesis$ext19_latent<-rowMeans(ext, na.rm=T)

#Agreeableness 2019-----------
gesis$agre19_1 <-car::recode(gesis$gdze002a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$agre19_2 <-6-car::recode(gesis$gdze007a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
agre <-data.frame(gesis$agre19_1, gesis$agre19_2)
gesis$agre19_latent<-rowMeans(agre, na.rm=T)

#Neuroticism 2019-----------
gesis$neu19_1 <-6-car::recode(gesis$gdze004a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
gesis$neu19_2 <-car::recode(gesis$gdze009a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA")
neu <-data.frame(gesis$neu19_1, gesis$neu19_2)
gesis$neu19_latent<-rowMeans(neu, na.rm=T)

psych::alpha(data.frame(gesis$con19_1, gesis$con19_2,gesis$open19_1, gesis$open19_2))
cor(gesis$con19_latent,gesis$open19_latent,use="pairwise.complete.obs")
gesis$openclosed19 <- rowMeans(data.frame(gesis$con19_1, gesis$con19_2,gesis$open19_1, gesis$open19_2),na.rm=T)

#Left-right ideology 2015-------------
gesis$lr15 <- car::recode(gesis$cbzc061a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA" )/10

#Left-right ideology 2017-------------
gesis$lr17 <- car::recode(gesis$ebzc061a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA" )/10

#Left-right ideology 2019-------------
gesis$lr19 <- car::recode(gesis$gbzc064a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA" )/10

#EU gone too far 2014
gesis$antiEU14 <- car::recode(gesis$bcaj063a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA; 98=NA")

#EU gone too far 2018
gesis$antiEU18 <- car::recode(gesis$efaj098a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; -11=NA; 98=NA")


#Female
gesis$female<-ifelse(gesis$a11d054a==-11, gesis$d11d054a, gesis$a11d054a)
gesis$female<-gesis$female-1

#Age
gesis$a11d056b<- 2013 - car::recode(gesis$a11d056b, "-99=NA; -11=NA")
gesis$d11d056b<- 2016 - car::recode(gesis$d11d056b, "-99=NA; -11=NA")
gesis$age<- ifelse(is.na(gesis$a11d056b)==TRUE, gesis$d11d056b, gesis$a11d056b)

#Education
gesis$a11d082b <-car::recode(gesis$a11d082b, "-99=NA; -11=NA")
gesis$d11d082a <-car::recode(gesis$d11d082a, "-99=NA; -11=NA")
gesis$education<-ifelse(is.na(gesis$a11d082b)==TRUE, gesis$d11d082a, gesis$a11d082b)
#1=no school/primary; 2=secondary; 3=polytechnic secondary; 4=advanced technical; 5=general unviersity; 6=other/student.
gesis$education<-car::recode(gesis$education, "2=1; 3=1; 4=2; 5=3; 6=3; 7=4; 8=5; 9=6; 1=6")

#Income
gesis$income<- car::recode(gesis$a11d097c, "-99=NA; -88=NA; -11=NA; 98=NA")
gesis$income<- ifelse(is.na(gesis$income)==TRUE, -1, gesis$income)

#income missing
gesis$income_missing<-ifelse(gesis$income==2, 1, 0)
#recode variables
gesis$open15_latent<-1-zero1(gesis$open15_latent)
gesis$open17_latent<-1-zero1(gesis$open17_latent)
gesis$open19_latent<-1-zero1(gesis$open19_latent)
gesis$con15_latent<-zero1(gesis$con15_latent)
gesis$con17_latent<-zero1(gesis$con17_latent)
gesis$con19_latent<-zero1(gesis$con19_latent)
gesis$lr15<-zero1(gesis$lr15)
gesis$lr17<-zero1(gesis$lr17)
gesis$lr19<-zero1(gesis$lr19)
gesis$antiEU14 <- zero1(gesis$antiEU14)
gesis$antiEU18 <- zero1(gesis$antiEU18)
gesis$ext15_latent<-zero1(gesis$ext15_latent)
gesis$ext17_latent<-zero1(gesis$ext17_latent)
gesis$ext19_latent<-zero1(gesis$ext19_latent)
gesis$agre15_latent<-zero1(gesis$agre15_latent)
gesis$agre17_latent<-zero1(gesis$agre17_latent)
gesis$agre19_latent<-zero1(gesis$agre19_latent)
gesis$neu15_latent<-zero1(gesis$neu15_latent)
gesis$neu17_latent<-zero1(gesis$neu17_latent)
gesis$neu19_latent<-zero1(gesis$neu19_latent)
gesis$id <- 1:nrow(gesis)

#create long dataset for pooled models ---------
gesis_long <- data.table::melt(data.table(gesis),measure.vars=list(c("open15_latent","con15_latent"),c("open17_latent","con17_latent"),c("open19_latent","con19_latent")),value.name=c("p1","p2","p3"))
gesis_long$variable <- as.numeric(gesis_long$variable)

#code income and education dummies
gesis_long$income[gesis_long$income==-1]=NA
gesis_long$income4 <- as.numeric(gtools::quantcut(gesis_long$income))
gesis_long$income4[is.na(gesis_long$income4)]=0
gesis_long <- dummy_cols(gesis_long,select_columns = "education")
gesis_long <- dummy_cols(gesis_long,select_columns = "income4")

save(gesis_long,file="Data/gesis_cleaned.RData")
